package HBaseDemo.HBase2HBase;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class H2HMapper extends TableMapper<ImmutableBytesWritable, Put> {
    //<KEY OUT,VALUE OUT>===><ImmutableBytesWritable, Put>//写出数据类型

    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
       //将行键key的值封装在Put对象里
        Put put = new Put(key.get());
        //遍历表中的每一个cell值
        for (Cell cell : value.rawCells()) {
            //获取列簇info
            if("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){
                //获取列簇中的列名name
                if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
                    //把name的值封装在put里
                    put.add(cell);
                }else if("age".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
                    //把age的值封装在put里
                    put.add(cell);


                }


            }

        }
        //将表里的数据读取到写入到上下文
        context.write(key, put);
    }
}
